package com.dm.hdfxxt.planmanage.service.impl;

import com.dm.hdfxxt.planmanage.mapper.PlanMapper;
import com.dm.hdfxxt.planmanage.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor=Exception.class)
public class PlanServiceImpl implements PlanService {
    @Autowired
    private PlanMapper planMapper;

    /**
         * @Description 保存仓库信息
         * @Author zhangxue
         * @Date 11:19 2018/10/30
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> savePlan(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        int i = planMapper.savePlan(param);
        if(i==1){
            StatusMap.put("Status",1);//保存成功
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",0);//保存失败
        }
        return StatusMap;
    }

    /**
     * @Description 查询所有计划
     * @Author zhangxue
     * @Date 11:36 2018/10/30
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> queryAllPlan(Map<String, Object> param) {
        List<Map<String,Object>> ResultList = planMapper.queryAllPlan(param);
        Map<String,Object> countMap = planMapper.queryAllPlanCount(param);
        int c1Total = Integer.valueOf(countMap.get("total").toString());
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("ResultList",ResultList);
        resultMap.put("c1Total",c1Total);
        return resultMap;
    }

    /**
         * @Description 删除计划
         * @Author zhangxue
         * @Date 11:58 2018/11/1
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> delPlan(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        int i = planMapper.delPlan(param);
        if(i==1){
            StatusMap.put("Status",1);//删除成功
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",0);//删除失败
        }
        return StatusMap;
    }

    /**
     * @Description 查询所有计划
     * @Author zhangxue
     * @Date 11:36 2018/10/30
     * @Param
     * @retrun
     **/
    @Override
    public List<Map<String, Object>> queryStoreNumByPlanNo(Map<String, Object> param) {
        List<Map<String,Object>> ResultList = planMapper.queryStoreNumByPlanNo(param);
        int DEHProductNumber = 0;
        Double DEHProductPrice = 0.00;
        int JHProductNumber = 0;
        Double JHProductPrice = 0.00;
        int JLLProductNumber = 0;
        Double JLLProductPrice = 0.00;
        int BDProductNumber = 0;
        Double BDProductPrice = 0.00;
        int BEHProductNumber = 0;
        Double BEHProductPrice = 0.00;
        int XEHProductNumber = 0;
        Double XEHProductPrice = 0.00;
        for(int i=0;i<ResultList.size();i++){
            String Store = ResultList.get(i).get("Store").toString();
            if(Store.equals("东二环店")){
                int ProductNumber = Integer.valueOf(ResultList.get(i).get("ProductNumber").toString());
                Double ProductPrice = Double.valueOf(ResultList.get(i).get("ProductPrice").toString());
                Double tempProductPrice = ProductNumber * ProductPrice;
                DEHProductNumber += ProductNumber;
                DEHProductPrice += tempProductPrice;
            }
            if(Store.equals("建华店")){
                int ProductNumber = Integer.valueOf(ResultList.get(i).get("ProductNumber").toString());
                Double ProductPrice = Double.valueOf(ResultList.get(i).get("ProductPrice").toString());
                Double tempProductPrice = ProductNumber * ProductPrice;
                JHProductNumber += ProductNumber;
                JHProductPrice += tempProductPrice;
            }
            if(Store.equals("金利来店")){
                int ProductNumber = Integer.valueOf(ResultList.get(i).get("ProductNumber").toString());
                Double ProductPrice = Double.valueOf(ResultList.get(i).get("ProductPrice").toString());
                Double tempProductPrice = ProductNumber * ProductPrice;
                JLLProductNumber += ProductNumber;
                JLLProductPrice += tempProductPrice;
            }
            if(Store.equals("北杜店")){
                int ProductNumber = Integer.valueOf(ResultList.get(i).get("ProductNumber").toString());
                Double ProductPrice = Double.valueOf(ResultList.get(i).get("ProductPrice").toString());
                Double tempProductPrice = ProductNumber * ProductPrice;
                BDProductNumber += ProductNumber;
                BDProductPrice += tempProductPrice;
            }
            if(Store.equals("北二环店")){
                int ProductNumber = Integer.valueOf(ResultList.get(i).get("ProductNumber").toString());
                Double ProductPrice = Double.valueOf(ResultList.get(i).get("ProductPrice").toString());
                Double tempProductPrice = ProductNumber * ProductPrice;
                BEHProductNumber += ProductNumber;
                BEHProductPrice += tempProductPrice;
            }
            if(Store.equals("西二环店")){
                int ProductNumber = Integer.valueOf(ResultList.get(i).get("ProductNumber").toString());
                Double ProductPrice = Double.valueOf(ResultList.get(i).get("ProductPrice").toString());
                Double tempProductPrice = ProductNumber * ProductPrice;
                XEHProductNumber += ProductNumber;
                XEHProductPrice += tempProductPrice;
            }
        }
        Map<String,Object> DEHMap = new HashMap<String, Object>();
        DEHMap.put("Store","东二环店");
        DEHMap.put("ProductNumber",DEHProductNumber);
        DEHMap.put("ProductPrice",DEHProductPrice);
        Map<String,Object> JHMap = new HashMap<String, Object>();
        JHMap.put("Store","建华店");
        JHMap.put("ProductNumber",JHProductNumber);
        JHMap.put("ProductPrice",JHProductPrice);
        Map<String,Object> JLLMap = new HashMap<String, Object>();
        JLLMap.put("Store","金利来店");
        JLLMap.put("ProductNumber",JLLProductNumber);
        JLLMap.put("ProductPrice",JLLProductPrice);
        Map<String,Object> BDMap = new HashMap<String, Object>();
        BDMap.put("Store","北杜店");
        BDMap.put("ProductNumber",BDProductNumber);
        BDMap.put("ProductPrice",BDProductPrice);
        Map<String,Object> BEHMap = new HashMap<String, Object>();
        BEHMap.put("Store","北二环店");
        BEHMap.put("ProductNumber",BEHProductNumber);
        BEHMap.put("ProductPrice",BEHProductPrice);
        Map<String,Object> XEHMap = new HashMap<String, Object>();
        XEHMap.put("Store","西二环店");
        XEHMap.put("ProductNumber",XEHProductNumber);
        XEHMap.put("ProductPrice",XEHProductPrice);
        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
        list.add(DEHMap);
        list.add(JHMap);
        list.add(JLLMap);
        list.add(BDMap);
        list.add(BEHMap);
        list.add(XEHMap);
        return list;
    }

}
